package com.yanqu.road.logic.activity.tomb.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityTableExt;
import com.yanqu.road.logic.activity.tomb.config.entity.*;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class TombConfig {

    private ActivityTombConfig config = new ActivityTombConfig();
    private ActivityTombEvent[] events = {};

    private Map<Integer, EventInfo> eventInfoMap = new HashMap<>();
    private Map<Integer, List<ActivityTombEvent>> eventListMap = new HashMap<>();
    private ActivityTombFloor[] floors = {};
    private ActivityTombRepair[] repairs = {};

    private ActivityTombBookmark[] bookmarks = {};

    private ActivityTombRank[] ranks = {};

    private int maxRank = 0;

    private int doorNum;
    private int repairItemProgress;

    private int initMaxFloor;

    private List<Integer> autoSwitch = new ArrayList<>();



    public ActivityTombConfig getConfig() {
        return config;
    }

    public TombConfig(Map<String, ActivityConfig> activityConfigMap, List<ActivityTableExt> activityTableExtList) {
        this.config = ActivityConfig.parseActivityConfig(activityConfigMap, ActivityTombConfig.class);

        this.doorNum = this.config.getTOMB_BORN_AREA_NUM();

        //7.2新增 玩家开启打理需要直接设置打理开关
        String autoTendNum = this.config.getAUTO_TEND_NUM();
        if (!StringUtils.isNullOrEmpty(autoTendNum)){
            this.autoSwitch = StringUtils.stringToIntegerList(autoTendNum,";");
        }else {
            this.autoSwitch = new ArrayList<>();
        }



        for (ActivityTableExt activityTableExt : activityTableExtList) {
            if (activityTableExt.getTableName().equals("t_s_activity_tomb_event")) {
                ActivityTombEvent[] activityTombEvent = ActivityTableExt.parseActivityTable(activityTableExt.getExt(), ActivityTombEvent[].class);
                if (activityTombEvent != null) {
                    this.events = activityTombEvent;
                    for (ActivityTombEvent tombEvent : activityTombEvent) {
                        if (!this.eventListMap.containsKey(tombEvent.getEventType())) {
                            this.eventListMap.put(tombEvent.getEventType(), new ArrayList<>());
                        }
                        List<ActivityTombEvent> activityTombEvents = this.eventListMap.get(tombEvent.getEventType());
                        activityTombEvents.add(tombEvent);
                    }
                }
            } else if (activityTableExt.getTableName().equals("t_s_activity_tomb_floor")) {
                ActivityTombFloor[] activityTombFloor = ActivityTableExt.parseActivityTable(activityTableExt.getExt(), ActivityTombFloor[].class);
                if (activityTombFloor != null) {
                    for (ActivityTombFloor tombFloor : activityTombFloor) {
                        if (tombFloor.getFloor().get(1) == -1) {
                            this.setInitMaxFloor(tombFloor.getFloor().get(0));
                        }
                    }
                    this.floors = activityTombFloor;
                }
            } else if (activityTableExt.getTableName().equals("t_s_activity_tomb_repair")) {
                ActivityTombRepair[] activityTombRepair = ActivityTableExt.parseActivityTable(activityTableExt.getExt(), ActivityTombRepair[].class);
                if (activityTombRepair != null) {
                    this.repairs = activityTombRepair;
                }
            } else if (activityTableExt.getTableName().equals("t_s_activity_tomb_bookmark")) {
                ActivityTombBookmark[] activityTombBookmarks = ActivityTableExt.parseActivityTable(activityTableExt.getExt(), ActivityTombBookmark[].class);
                if (activityTombBookmarks != null) {
                    this.bookmarks = activityTombBookmarks;
                }
            } else if (activityTableExt.getTableName().equals("t_s_activity_rank")) {
                ActivityTombRank[] activityTombRanks = ActivityTableExt.parseActivityTable(activityTableExt.getExt(), ActivityTombRank[].class);
                if (activityTombRanks != null) {
                    this.ranks = activityTombRanks;
                }
            }
        }
    }

    public ActivityTombEvent[] getEvents() {
        return events;
    }

    public ActivityTombFloor[] getFloors() {
        return floors;
    }

    public ActivityTombRepair[] getRepairs() {
        return repairs;
    }

    public ActivityTombBookmark[] getBookmarks() {
        return bookmarks;
    }


    public ActivityTombEvent getEvent(int eventId) {
        for (ActivityTombEvent event : this.events) {
            if (event.getId() == eventId) {
                return event;
            }
        }
        return null;
    }

    public List<ActivityTombEvent> getEvents(int type) {
        List<ActivityTombEvent> list = new ArrayList<>();
        for (ActivityTombEvent event : this.events) {
            if (event.getEventType() == type) {
                list.add(event);
            }
        }
        return list;
    }



    public int getDoorNum() {
        return doorNum;
    }

    public int getRepairItemProgress() {
        return repairItemProgress;
    }

    public void setRepairItemProgress(int repairItemProgress) {
        this.repairItemProgress = repairItemProgress;
    }


    public ActivityTombRank[] getRanks() {
        return ranks;
    }


    public ActivityTombFloor getFloor(int floor) {
        for (ActivityTombFloor activityTombFloor : this.floors) {
            if (floor >= activityTombFloor.getFloor().get(0) && (floor <= activityTombFloor.getFloor().get(1) || activityTombFloor.getFloor().get(1) == -1)) {
                return activityTombFloor;
            }
        }
        return null;
    }



    public ActivityTombEvent getBoss(int floor) {
        List<ActivityTombEvent> activityTombEvents = this.getEvents(4);
        return activityTombEvents.get(0);
    }

    public int getTotalBigGrip(List<EventInfo> eventInfoList) {
        int total = 0;

        for (EventInfo eventInfo : eventInfoList) {
            ActivityTombEvent event = this.getEvent(eventInfo.getEventId());
            total = total + eventInfo.getGenerateNum() * event.getArea();

        }

        if (total % 9 == 0) {
            return total / 9;
        } else {
            return total / 9 + 1;
        }
    }

    public int getTotalNormalEventNum(List<EventInfo> eventInfoList) {
        int total = 0;
        for (EventInfo eventInfo : eventInfoList) {
            ActivityTombEvent activityTombEvent = this.getEvent(eventInfo.getEventId());
            if (!activityTombEvent.isBigEvent()) {
                total = total + eventInfo.getGenerateNum();
            }
        }
        return total;
    }

    public int getTotalBigEventNum(List<EventInfo> eventInfoList) {
        int total = 1;
        for (EventInfo eventInfo : eventInfoList) {
            ActivityTombEvent activityTombEvent = this.getEvent(eventInfo.getEventId());
            if (activityTombEvent.isBigEvent()) {
                total = total + eventInfo.getGenerateNum();
            }
        }
        return total;
    }


    public int getBuffEventNum(List<EventInfo> eventInfoList) {
        int total = 0;
        for (EventInfo eventInfo : eventInfoList) {
            ActivityTombEvent activityTombEvent = this.getEvent(eventInfo.getEventId());
            if (activityTombEvent.getEventType() == Tomb.EventEnum.EVENT_BUFF_VALUE) {
                total = total + eventInfo.getGenerateNum();
            }
        }
        return total;
    }


    public List<EventInfo> generateEventWeight(int floor, int totalEventNum) {
        ActivityTombFloor activityTombFloor = this.getFloor(floor);
        List<EventInfo> eventInfoList = new ArrayList<>();
        totalEventNum = totalEventNum - activityTombFloor.getBuffEventNum();

        //精英事件，写死一个
        totalEventNum = totalEventNum - 1;
        int generateEventNum = 0;
        int totalWeight = 0;
        for (List<Integer> integerList : activityTombFloor.getEventWeight()) {
            totalWeight = totalWeight + integerList.get(1);
        }

        for (List<Integer> integerList : activityTombFloor.getEventWeight()) {
            int eventId = integerList.get(0);
            int num = integerList.get(1);

            if (num == 0) {
                continue;
            }
            int generateNum = (int) ((double) num / (double) totalWeight * (double) totalEventNum);
            if (generateNum == 0) {
                continue;
            }
            EventInfo eventInfo = new EventInfo();
            eventInfo.setEventId(eventId);
            eventInfo.setGenerateNum(generateNum);
            generateEventNum = generateEventNum + generateNum;
            eventInfoList.add(eventInfo);
        }
        int dif = totalEventNum - generateEventNum;
        if (dif > 0) {
            ActivityTombEvent suppleEvent = this.getSuppleEvent();
            EventInfo eventInfo = null;
            for (EventInfo info : eventInfoList) {
                if (info.getEventId() == suppleEvent.getId()) {
                    eventInfo = info;
                }
            }
            if (eventInfo == null) {
                eventInfo = new EventInfo();
                eventInfo.setEventId(suppleEvent.getId());
                eventInfoList.add(eventInfo);
            }
            eventInfo.setGenerateNum(eventInfo.getGenerateNum() + dif);
        }
        return eventInfoList;
    }

    public int getInitMaxFloor() {
        return initMaxFloor;
    }

    public void setInitMaxFloor(int initMaxFloor) {
        this.initMaxFloor = initMaxFloor;
    }


    public ActivityTombEvent getSuppleEvent() {
        List<ActivityTombEvent> activityTombEventList = this.getEvents(8);
        return activityTombEventList.get(activityTombEventList.size() - 1);
    }

    public int getMaxRank() {
        return maxRank;
    }

    public void setMaxRank(int maxRank) {
        this.maxRank = maxRank;
    }


    public List<Integer> getAutoSwitch() {
        return autoSwitch;
    }

    public void setAutoSwitch(List<Integer> autoSwitch) {
        this.autoSwitch = autoSwitch;
    }
}
